<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\Gift;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Praises extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->param();
        isset($data['status']) ? $data['status'] : $data['status'] = 1;
        $data['listrow'] = 30;
        $gift = new Gift();
        $res = $gift->lists($data);
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(0,'无数据');
        }

    }

    /**
     * 点赞记录
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        $data = $request->param();
        self::checktoken($data['id']);
        $status = \db('home_praises')->where(['sqid'=>$data['sqid'],'uid'=>$data['id']])->field('status')->find();
        if ($status['status'] == 2){
            \db('home_praises')->where(['sqid'=>$data['sqid'],'uid'=>$data['id']])->update(['status'=>1]);
            $count = \db('home_praises')->where(['sqid'=>$data['sqid'],'status'=>1])->count();
            return apistatus(1,'点赞成功',$count);
        }elseif($status['status'] == 1){
            \db('home_praises')->where(['sqid'=>$data['sqid'],'uid'=>$data['id']])->update(['status'=>2]);
            $count = \db('home_praises')->where(['sqid'=>$data['sqid'],'status'=>1])->count();
            return apistatus(1,'取消成功',$count);
        }

        $insert['uid'] = $data['id'];
        $insert['sqid'] = $data['sqid'];
        $insert['create_time'] = time();
        $res = \db('home_praises')->insert($insert);
        if ($res){
            $count = \db('home_praises')->where(['sqid'=>$data['sqid'],'status'=>1])->count();
            $id = \db('home_squares')->where('id',$data['sqid'])->field('uid')->find();
            $news['fromid'] = $data['id'];
            $news['toid'] = $id['uid'];
            $news['news_content'] = '点赞了您的动态';
            $news['create_time'] = time();
            $news['type'] = 2;
            $news['sqid'] = $data['sqid'];
            \db('home_news')->insert($news);
            return apistatus(1,'点赞成功',$count);
        }else{
            return apistatus(0,'网络错误请重试');
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request)
    {

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    

}
